//
//  MNDChangePhoneViewController.m
//  minidai
//
//  Created by Steven.Yung on 2017/5/8.
//  Copyright © 2017年 迷你金服. All rights reserved.
//

#import "MNDChangePhoneViewController.h"
#import "UIImage+JKColor.h"
#import "UIButton+JKCountDown.h"
#import "NSString+JKNormalRegex.h"

@interface MNDChangePhoneViewController ()
@property (weak, nonatomic) IBOutlet UITextField *userPhoneTextField;
@property (weak, nonatomic) IBOutlet UITextField *captchatextField;
@property (weak, nonatomic) IBOutlet UIButton *captchaBtn;
@property (weak, nonatomic) IBOutlet UIButton *submitBtn;

@property (copy, nonatomic) NSString *userPhone;
@property (copy, nonatomic) NSString *captcha;

@end

@implementation MNDChangePhoneViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    RAC(self, userPhone) = self.userPhoneTextField.rac_textSignal;
    RAC(self, captcha)   = self.captchatextField.rac_textSignal;
    
    [self.submitBtn setBackgroundImage:[UIImage imageNamed:@"invest_result_continue_btn"] forState:UIControlStateNormal];
    [self.submitBtn setBackgroundImage:[UIImage jk_imageWithColor:kColorLightGray] forState:UIControlStateDisabled];
    
    RAC(self.captchaBtn, enabled) = [RACSignal combineLatest:@[RACObserve(self, userPhone)] reduce:^id (NSString *phone){
        
        return @(phone.length > 0);
    }];
    
    RAC(self.submitBtn, enabled) = [RACSignal combineLatest:@[RACObserve(self, userPhone),
                                                              RACObserve(self, captcha)] reduce:^id(NSString *phone, NSString *captch){
                                                                  
                                                                  return @(phone.length > 0 && captch.length > 0);
                                                              }];
    
    __block UITextField *phoneTextField = self.userPhoneTextField;
    [self.userPhoneTextField.rac_textSignal subscribeNext:^(NSString * _Nullable x) {
        if (x.length > 11) {
            phoneTextField.text = [x substringToIndex:11];
        }
    }];
    
    __block UITextField *captchaTextField = self.captchatextField;
    [self.captchatextField.rac_textSignal subscribeNext:^(NSString * _Nullable x) {
        if (x.length > 6) {
            captchaTextField.text = [x substringToIndex:6];
        }
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)sendPhoneCaptchaAction:(id)sender {
    
    [NetworkProgressHUD showHud];
    NSDictionary *params = [MNDSign signedParamWithDict:@{@"user_phone": self.userPhone}];
    
    [APIManager sendRequestWithPath:API_SEND_NEW_PHONE_CAPTCHA parameters:params method:kXMHTTPMethodPOST onSuccess:^(ResponseModel *responseModel) {
        if (responseModel.status == ResponseSuccess) {
            [NetworkProgressHUD showHudMsg:@"验证码已发送"];
            UIButton *btn = sender;
            [btn jk_startTime:30 title:@"重新获取" waitTittle:@"秒后重发"];
        } else {
            NSDictionary *dict = responseModel.msg;
            if (dict && dict.count > 0) {
                [NetworkProgressHUD showHudMsg:dict[@"error"]];
                
            } else {
                [NetworkProgressHUD showHudMsg:@"获取数据失败"];
            }
        }
    } onFailure:^(NSError *error) {
        [NetworkProgressHUD showHudNetworkError:error];
    }];
}
- (IBAction)submitAction:(id)sender {

    WeakSelf(self);
    [NetworkProgressHUD showHud];
    NSDictionary *params = [MNDSign signedParamWithDict:@{@"user_phone": self.userPhone, @"phone_captcha": self.captcha}];
    
    [APIManager sendRequestWithPath:API_PHONE_UPDATE parameters:params method:kXMHTTPMethodPOST onSuccess:^(ResponseModel *responseModel) {
        if (responseModel.status == ResponseSuccess) {
            [NetworkProgressHUD showHudMsg:@"修改成功"];
            
            [weakself performSelector:@selector(popToRootVC) withObject:nil afterDelay:2.0];
        } else {
            NSDictionary *dict = responseModel.msg;
            if (dict && dict.count > 0) {
                [NetworkProgressHUD showHudMsg:dict[@"error"]];
                
            } else {
                [NetworkProgressHUD showHudMsg:@"获取数据失败"];
            }
        }
    } onFailure:^(NSError *error) {
        [NetworkProgressHUD showHudNetworkError:error];
    }];
}

- (void)popToRootVC {
    UIViewController *vc = self.navigationController.viewControllers[1];
    [self.navigationController popToViewController:vc animated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
